#include "StdAfx.h"
#include "FlashProxy.h"
#include "sstream"
#include "string"

#define ROLE_UNKNOW  -1
#define ROLE_EMCEE   0
#define ROLE_PLAYER  1
#define ROLE_LOOKON  2

#ifndef _UNICODE 
#define RETURN_FLASH_RESULT(result) {\
	result.MakeLower();\
	result.Replace(_T("<number>"),_T(""));\
	result.Replace(_T("</number>"),_T(""));\
	return atoi(result);\
}
#else
#define RETURN_FLASH_RESULT(result) {\
	result.MakeLower();\
	result.Replace(_T("<number>"),_T(""));\
	result.Replace(_T("</number>"),_T(""));\
	char tmp[1024] = {0};\
	WideCharToMultiByte(936, NULL, result, -1, tmp, 1024, NULL, FALSE);\
	return atoi(tmp);\
}
#endif

using namespace std;
FlashProxy::FlashProxy(CShockwaveFlash& flash):m_flash(flash)
{
}

FlashProxy::~FlashProxy(void)
{
}

CString  FlashProxy::GetChallenge(LPCTSTR dominId)
{
	CString cmd;
	cmd.Format(_T("<invoke name=\"GetChallenge\">\
		<arguments>\
		<string>%s</string>\
		</arguments>\
		</invoke>"),
		dominId);

	try
	{
		CString result = m_flash.CallFunction(cmd);
		return result;
	}
	catch(...)
	{
		return _T("");
	}
}

int FlashProxy::LoginRoom(LPCTSTR doman, LPCTSTR authInfo, LPCTSTR token)
{
	CString cmd;
	cmd.Format(_T("<invoke name=\"LoginRoom\">\
		<arguments>\
		<string>%s</string>\
		<string>%s</string>\
		<string>%s</string>\
		</arguments></invoke>"),
		doman, authInfo, token);

	try
	{
		CString result = m_flash.CallFunction(cmd);
		RETURN_FLASH_RESULT(result);
	}
	catch(...)
	{
		return ERROR_EXCEPTION;
	}
}

int FlashProxy::ExitRoom()
{
	CString cmd;
	cmd.Format(_T("<invoke name=\"ExitRoom\"></invoke>"));

	try
	{
		CString result = m_flash.CallFunction(cmd);
		RETURN_FLASH_RESULT(result);
	}
	catch(...)
	{
		return ERROR_EXCEPTION;
	}
}


int FlashProxy::SetRoomParam(LPCTSTR param)
{
	CString cmd;
	cmd.Format(_T("<invoke name=\"SetRoomParam\"><arguments>\
		<string>%s</string>\
		</arguments></invoke>"),
		param);

	try
	{
		CString result = m_flash.CallFunction(cmd);
		RETURN_FLASH_RESULT(result);
	}
	catch(...)
	{
		return ERROR_EXCEPTION;
	}
}

CString FlashProxy::GetRoomParam(LPCTSTR paramName)
{
	CString cmd;
	cmd.Format(_T("<invoke name=\"GetRoomParam\"><arguments>\
		<string>%s</string>\
		</arguments></invoke>"),
		paramName);

	try
	{
		CString result = m_flash.CallFunction(cmd);
		return result;
	}
	catch(...)
	{
		return _T("");
	}
}
int FlashProxy::SetTaboo(LPCTSTR Taboo)
{
	CString cmd;
	cmd.Format(_T("<invoke name=\"SetTaboo\"><arguments>\
		<string>%s</string>\
		</arguments></invoke>"),
		Taboo);

	try
	{
		CString result = m_flash.CallFunction(cmd);
		RETURN_FLASH_RESULT(result);
	}
	catch(...)
	{
		return ERROR_EXCEPTION;
	}
}

CString FlashProxy::GetTaboo()
{
//??	cmd << "<invoke name=\"GetTaboo\"></arguments></invoke>";
	CString cmd;
	cmd.Format(_T("<invoke name=\"GetTaboo\"></invoke>"));

	try
	{
		CString result = m_flash.CallFunction(cmd);
		return result;
	}
	catch(...)
	{
		return _T("");
	}
}


int FlashProxy::AddCommand(LPCTSTR cmdID,LPCTSTR cmdText,int position)
{
	CString cmd;
	cmd.Format(_T("<invoke name=\"AddCommand\">\
				  <arguments>\
				  <string>%s</string>\
				  <string>%s</string>\
				  <number>%d</number>\
				  </arguments></invoke>"),
				  cmdID, cmdText, position);

	try
	{
		CString result = m_flash.CallFunction(cmd);
		RETURN_FLASH_RESULT(result);
	}
	catch(...)
	{
		return ERROR_EXCEPTION;
	}
}

int FlashProxy::RemoveCommand(LPCTSTR cmdID)
{
	CString cmd;
	cmd.Format(_T("<invoke name=\"RemoveCommand\">\
				  <arguments>\
				  <string>%s</string>\
				  </arguments></invoke>"),
				  cmdID);

	try
	{
		CString result = m_flash.CallFunction(cmd);
		RETURN_FLASH_RESULT(result);
	}
	catch(...)
	{
		return ERROR_EXCEPTION;
	}

}

void FlashProxy::OnLoginOver(int result)
{
	int i=0;
	i++;
}

void FlashProxy::OnInvokeCommand(LPCTSTR cmdID)
{
	int i=0;
	i++;
}

void FlashProxy::OnExitRoom(int reasion)
{
	int i=0;
	i++;
}